﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Pageparkproject.Models;
using System.Web.Mvc;

namespace Pageparkproject.Models
{
    public class AdminStories
    {
        PageParkDBEntities Db = new PageParkDBEntities();
        int adminId = 2;
        string IPAddress;

        #region [tblAdmin]
        public tblAdmin adminFindByUsername(string username)
        {
            return Db.tblAdmins.First(m => m.sUsername == username);
        }
        public bool loginAdmin(string username, string password,string IP)
        {
            IPAddress = IP;
            try
            {
                tblAdmin userlogin = new tblAdmin();
                userlogin = adminFindByUsername(username);
                if (userlogin.sPassword.TrimEnd() == password.TrimEnd())
                {
                    adminId = adminFindByUsername(username).PK_iAdminID;
                    saveLog("Login Done!");
                    return true;
                }
                else
                {
                    saveLog("Login False");
                    return false;
                }
            }
            catch
            {
                saveLog("Login False");
                return false;
            }
        }
        #endregion

        #region [tblLog]
        public void saveLog(string Log)
        {
            tblLog lg = new tblLog();
            lg.dTime= DateTime.Now;
            lg.FK_iAdminID=adminId;
            lg.sLog ="IP: "+IPAddress +"; Log:  "+ Log;
            Db.tblLogs.Add(lg);
            Db.SaveChanges();
        }
        public void deleteLog()
        {
            List<tblLog> listItem = Db.tblLogs.OrderByDescending(m=>m.dTime).ToList();
            foreach (var item in listItem)
            {
                if (DateTime.Now.CompareTo(item.dTime) >= 30)
                {
                    Db.tblLogs.Remove(item);
                }
                else
                {
                    break;
                }
            }
        }
        public List<tblLog> logList()
        {
            deleteLog();
            return Db.tblLogs.OrderByDescending(m=>m.PK_iLogID).ToList();
        }
        #endregion

        #region [tblProject]
        public List<tblProject> projectList()
        {
            return Db.tblProjects.OrderByDescending(m=>m.PK_iProjectID).ToList();
        }

        public tblProject projectGetById(int id)
        {
            return projectList().FirstOrDefault(m => m.PK_iProjectID == id);
        }
        public List<tblImageProject> projectGetImagesById(int id)
        {
            return Db.tblImageProjects.Where(m => m.FK_iProjectID == id).ToList();
        }

        public void projectCreate(tblProject item)
        {
            Db.tblProjects.Add(item);
            Db.SaveChanges();
            saveLog("Create Project: "+item.sProjectName);
        }

        public void projectEdit(tblProject item)
        {
            var itemEdit = Db.tblProjects.Single(m => m.PK_iProjectID == item.PK_iProjectID);
            itemEdit.sDescription = item.sDescription;
            itemEdit.sProjectName = item.sProjectName;
            itemEdit.FK_iCategoryID = item.FK_iCategoryID;
            itemEdit.dNgay = item.dNgay;
            itemEdit.bStatus = item.bStatus;
            if (item.sAnh != null) { itemEdit.sAnh = item.sAnh; };
            Db.SaveChanges();
            saveLog("Edit Project:"+item.sProjectName);
        }

        public void projectDelete(int id)
        {
            var item = Db.tblProjects.Single(m => m.PK_iProjectID == id);
            Db.tblProjects.Remove(item);
            Db.SaveChanges();
            saveLog("Delete Project :"+item.sProjectName);
        }
        #endregion

        #region [tblCategory]
        public List<SelectListItem> categoryGetSelectList()
        {
            List<SelectListItem> listItem = new List<SelectListItem>();
            foreach (var item in Db.tblCategories.ToList())
            {
                listItem.Add(new SelectListItem() { Value = item.PK_iCategoryID.ToString(), Text = item.sCategoryName });
            }
            return listItem;
        }
        public List<tblCategory> categoryList()
        {
            return Db.tblCategories.ToList();
        }
        public tblCategory categoryGetById(int id)
        {
            return Db.tblCategories.First(m=>m.PK_iCategoryID==id);
        }
        public void categoryCreate(tblCategory item)
        {
            Db.tblCategories.Add(item);
            Db.SaveChanges();
        }
        public void categoryEdit(tblCategory item)
        {
            var itemEdit = Db.tblCategories.Single(m => m.PK_iCategoryID == item.PK_iCategoryID);
            itemEdit.sCategoryName = item.sCategoryName;
            itemEdit.bStatus = item.bStatus;
            Db.SaveChanges();
        }
        public void categoryDelete(int id)
        {
            var item = Db.tblCategories.Single(m => m.PK_iCategoryID == id);
            Db.tblCategories.Remove(item);
            Db.SaveChanges();
        }
        #endregion

        #region [tblNewsCategory]
        public List<SelectListItem> categoryNewsGetSelectList()
        {
            List<SelectListItem> listItem = new List<SelectListItem>();
            foreach (var item in Db.tblCategoryNews.ToList())
            {
                listItem.Add(new SelectListItem() { Value = item.PK_iCategoryNewsID.ToString(), Text = item.sCategoryName });
            }
            return listItem;
        }
        public List<tblCategoryNew> categoryNewsList()
        {
            return Db.tblCategoryNews.ToList();
        }
        public tblCategoryNew categoryNewsGetById(int id)
        {
            return Db.tblCategoryNews.First(m => m.PK_iCategoryNewsID == id);
        }
        public void categoryNewsCreate(tblCategoryNew item)
        {
            Db.tblCategoryNews.Add(item);
            Db.SaveChanges();
        }
        public void categoryNewsEdit(tblCategoryNew item)
        {
            var itemEdit = Db.tblCategoryNews.Single(m => m.PK_iCategoryNewsID == item.PK_iCategoryNewsID);
            itemEdit.sCategoryName = item.sCategoryName;
            itemEdit.bStatus = item.bStatus;
            Db.SaveChanges();
        }
        public void categoryNewsDelete(int id)
        {
            var item = Db.tblCategoryNews.Single(m => m.PK_iCategoryNewsID == id);
            Db.tblCategoryNews.Remove(item);
            Db.SaveChanges();
        }
        #endregion

        #region [tblImagesProject]
        public void imagesProjectCreate(tblImageProject item)
        {
            Db.tblImageProjects.Add(item);
            Db.SaveChanges();
        }
        public tblImageProject imageProjectGetByImageId(int id)
        {
            return Db.tblImageProjects.FirstOrDefault(m=>m.PK_iImageID==id);
        }
        public void imagesProjectEditName(tblImageProject item)
        {
            Db.tblImageProjects.Single(m => m.PK_iImageID == item.PK_iImageID).sImages=item.sImages;
            Db.SaveChanges();
        }
        public void imagesProjectDelete(int id)
        {
            var item = Db.tblImageProjects.Single(m => m.PK_iImageID == id);
            Db.tblImageProjects.Remove(item);
            Db.SaveChanges();
        }
        #endregion

        #region [tblNews]
        public List<tblNew> newsList()
        {
            return Db.tblNews.OrderByDescending(m=>m.PK_iNewsID).ToList();
        }
        public tblNew newsGetById(int id)
        {
            return newsList().FirstOrDefault(m => m.PK_iNewsID == id);
        }
        public void newsCreate(tblNew item)
        {
            item.dDatepost = DateTime.Now;
            Db.tblNews.Add(item);
            Db.SaveChanges();
            saveLog("Create News:" + item.sTieude);
        }
        public void newsEdit(tblNew item)
        {
            var itemEdit = Db.tblNews.Single(m => m.PK_iNewsID == item.PK_iNewsID);
            if (item.sAnh!=null) itemEdit.sAnh = item.sAnh;
            itemEdit.FK_iCategoryNewsID = item.FK_iCategoryNewsID;
            itemEdit.sNoidung = item.sNoidung;
            itemEdit.sTieude = item.sTieude;
            itemEdit.sTomtat = item.sTomtat;
            itemEdit.bStatus = item.bStatus;
            Db.SaveChanges();
            saveLog("Edit News:" + item.sTieude);
        }
        public void newsDelete(int id)
        {
            var item = Db.tblNews.Single(m => m.PK_iNewsID == id);
            Db.tblNews.Remove(item);
            Db.SaveChanges();
            saveLog("Delete News:" + item.sTieude);
        }
        #endregion

        #region [tblSetting]
        public tblSetting getSetting()
        {
            return Db.tblSettings.First();
        }

        public void SettingEdit(tblSetting item)
        {
            if (item != null)
            {
                var itemedit = Db.tblSettings.Single(m => m.PK_iSettingID == 1);
                if (item.sContact != null) itemedit.sContact = item.sContact;
                if (item.sContentFooter != null) itemedit.sContentFooter = item.sContentFooter;
                if (item.sWebName != null) itemedit.sWebName = item.sWebName;
                if (item.fLatitude != null) itemedit.fLatitude = item.fLatitude;
                if (item.fLongtitude != null) itemedit.fLongtitude = item.fLongtitude;
                if (item.sImageGioithieu != null) itemedit.sImageGioithieu = item.sImageGioithieu;
                if (item.sImageIndex != null) itemedit.sImageIndex = item.sImageIndex;
                if (item.sColorBar != null) itemedit.sColorBar = item.sColorBar;
                if (item.sGioithieu != null) itemedit.sGioithieu = item.sGioithieu;
                Db.SaveChanges();
            }
        }
        #endregion

        #region [tblCategoryNews]
        public List<SelectListItem> getListCategoryNews()
        {
            List<SelectListItem> listItem = new List<SelectListItem>();
            foreach (var item in Db.tblCategoryNews.ToList())
            {
                listItem.Add(new SelectListItem() { Value = item.PK_iCategoryNewsID.ToString(), Text = item.sCategoryName });
            }
            return listItem;
        }
        #endregion
        #region []
        #endregion
        #region []
        #endregion
        #region []
        #endregion
        #region []
        #endregion
        #region []
        #endregion
        #region []
        #endregion
        #region []
        #endregion
        #region []
        #endregion
    }
}